<?php
/**
 * Created by PhpStorm.
 * User: 申霖
 * Date: 2019/8/25 0025
 * Time: 上午 10:58
 */

class Login extends CI_Controller
{
    /**
     * 登录
     * @author 申霖
     * @time 2019/8/27 0027 下午 8:34
     */
    public function home()
    {
        $this->load->view('login/index');
    }

    /**
     * 登录校验
     * @author 申霖
     * @time 2019/8/27 0027 下午 8:17
     */
    public function data()
    {
        $name = $this->input->post("name");
        $pass = $this->input->post("pass");
        if (!$name) {
            echo json_encode(array('code' => 100, 'msg' => '请填写账户'));
            die;
        }
        if (!$pass) {
            echo json_encode(array('code' => 102, 'msg' => '请填写密码'));
            die;
        }
        $this->db->select('id');
        $this->db->limit(1);
        $this->db->where(array('type' => 0, 'name' => $name, 'pass' => md5($pass)));
        $result = $this->db->get("user")->result_array();
        if ($result) {
            $this->session->set_userdata(array('userName' => $name));
            echo json_encode(array('code' => 200, 'msg' => '登录成功'));
        } else {
            echo json_encode(array('code' => 101, 'msg' => '账户或密码错误'));
        }
    }

    /**
     * 退出登录
     * @author 申霖
     * @time 2019/8/25 0025 上午 10:59
     */
    public function out()
    {
        $this->session->unset_userdata('userName');
        redirect('/');
    }
}